
let express = require('express');
let router = express.Router();
let filter = require('../../components/filter');

router.get('/1/2/3/4', function (req, res, next) {
    req.app.db.query("select * from menu", [], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        for (let i = 0; i < results.length * 100; i ++) {
            for (let j = 0; j < results.length * 100; j ++) {
                if (results[1].id === results[1].id) {
                    console.log(results[0]);
                }
            }
        }
        res.json({"success": true, "data": results, "message": ""});
    });
});
/**
 * 查询字典数据 2018-10-17
 *
 * 按条件查询字典数据
 *
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.query;

    var limits = "";
    var sql = "";
    var datas = [];

    if (params.type !== undefined && params.type !== "") {
        sql += " where type = ?";
        datas.push(params.type);
    }

    req.app.db.query("select count(1) as totalCount from dic " + sql, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results[0].totalCount === 0) {
            res.json({"success": true, "data": {}, "totalCount": 0});
            return;
        }

        let totalCount = results[0].totalCount;

        if (params.pageNo > 0 && params.pageSize > 0) {
            limits = " limit ?, ?";
            datas.push((params.pageNo - 1) * params.pageSize);
            datas.push(params.pageSize * 1);
        }

        req.app.db.query("select * from dic " + sql + " order by `type`, `order` desc " + limits, datas, function (err, results, fields) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }
            
            res.json({"success": true, "data": results, "totalCount": totalCount, "message": ""});
        });
    });
});

/**
 * 查看字典数据 2018-10-17
 *
 * 按id查看指定的字典信息
 *
 */
router.get('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from dic where id = ?", [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": false, "message": global.noDataErr});
            return;
        }

        res.json({"success": true, "data": results[0]});
    });
});

/**
 * 修改字典数据 2018-10-17
 *
 *
 */
router.put('/:id', filter.authorize, function (req, res, next) {
    let params = req.body;
    let user = req.session.user;
    let now = new Date();

    
    if (params.id === undefined || params.id === '') {
        res.json({"success": false, "message": "字典编码错误！"});
        return;
    }
    
    if (params.type === undefined || params.type === '' ) {
        res.json({"success": false, "message": "字典类型不能为空！"});
        return;
    }
    
    if (params.text === undefined || params.text === '' ) {
        res.json({"success": false, "message": "字典文本不能为空！"});
        return;
    }
    
    let value = 0;    
    if (params.value !== undefined && params.value !== "") {
        value = params.value;
    }
    
    let order = 0;    
    if (params.order !== undefined && params.order !== "") {
        order = params.order;
    }
    
    req.app.db.query("update dic set type = ?, text = ?, value = ?, `order` = ?, userId = ?, userName = ?, updateDate = ? where id = ?", [params.type, params.text, value, order, user.id, user.trueName, now, params.id], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.putMsg});
    });
});

/**
 * 新增字典数据 2018-10-17
 *
 *
 */
router.post('/', filter.authorize, function (req, res, next) {
    let params = req.body;
    let user = req.session.user;
    let now = new Date();
        
    if (params.type === undefined || params.type === '' ) {
        res.json({"success": false, "message": "字典类型不能为空！"});
        return;
    }
    
    if (params.text === undefined || params.text === '' ) {
        res.json({"success": false, "message": "字典文本不能为空！"});
        return;
    }
    
    let value = 0;    
    if (params.value !== undefined && params.value !== "") {
        value = params.value;
    }
    
    let order = 0;    
    if (params.order !== undefined && params.order !== "") {
        order = params.order;
    }

    req.app.db.query("insert into dic(type, text, value, `order`, userId, userName, createDate, updateDate) values(?, ?, ?, ?, ?, ?, ?, ?)", [params.type, params.text, value, order, user.id, user.trueName, now, now], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.postMsg});
    });
});
    
/**
 * 删除字典信息 2018-10-17
 *
 * 按id删除指定的字典信息
 */
router.delete('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query('delete from dic where id = ?', [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.deleteMsg});
    });
});
    
module.exports = router;
    